﻿using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using VOD.Models;
using VOD.ViewModels;

namespace VOD.Controllers
{
    public class OthersController : Controller
    {
        readonly VODEntities _db = new VODEntities();
        //
        // GET: /Others/

        public ActionResult Index()
        {
            return View();
        }

        // 首页大图幻灯片
        // GET: /Others/HomeSlide
        public ActionResult HomeSlide()
        {
            var videoHasBigImg = _db.Videos
                .Where(v => v.HasBigPic)
                .OrderByDescending(v => v.UpdateDate)
                .Take(7)
                .ToList();
            return View(videoHasBigImg);
        }

        /// <summary>
        /// 频道大图幻灯片
        /// </summary>
        /// <param name="id">CategoryID</param>
        /// <returns></returns>
        public ActionResult VideoSlide(int? id)
        {
            var video = _db.Videos
                    .Where(v => v.HasBigPic && v.Recycled != 1);
            if (id > 0)
            {
                video = video.Where(v => v.CategoryID == id);
            }
            return View(video.OrderByDescending(v => v.UpdateDate).Take(3));
        }

        // 页面脚部
        // GET: /Others/Footer
        public ActionResult Footer()
        {
            var footer = new Footer
                             {
                                 OnlineNum = Membership.GetNumberOfUsersOnline(),
                                 PV = _db.SiteCounts.Count(),
                                 CountScript = Helpers.VODConfig.CountScript
                             };
            return View(footer);
        }

        // 友情链接
        // GET: /Others/Links
        public ActionResult Links()
        {
            var links = _db.Links.OrderByDescending(l => l.AddDate).ToList();
            return View(links);
        }

        //调用Qvod播放器
        public ActionResult QvodPlayer(int id, int order = 1)
        {
            var url = _db.VideoUrls
                .OrderBy(f => f.VideoOrder)
                .Where(f => f.VideoID == id && f.VideoOrder >= order)
                .Take(1).ToList();
            if (url.Count >= 1)
            {
                ViewBag.Url = url[0].VideoURL;
            }
            ViewBag.ID = id;
            ViewBag.Order = order + 1;
            return View();
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}
